﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace E_mail
{
    /// <summary>
    /// Interaction logic for WindowContacts.xaml
    /// </summary>
    public partial class WindowContacts : Window
    {
        private ObservableCollection<Contact> selectedContacts;
        ObservableCollection<Contact> AllContacts;
        CollectionView _cv;

        bool _result;
        private readonly AccountData _accountData;
        #region .cor
        public WindowContacts()
        {
            InitializeComponent();
            btn_RemoveSelected.Content = "<=";
        }

        public WindowContacts(ObservableCollection<Contact> selectedContacts)
            : this()
        {
            this.selectedContacts = selectedContacts;
        }

        public WindowContacts(ObservableCollection<Contact> AllContacts, ObservableCollection<Contact> selectedContacts)
            : this()
        {
            // TODO: Complete member initialization
            this.AllContacts = AllContacts;
            this.selectedContacts = selectedContacts;

            listView_AllContacts.ItemsSource = AllContacts;
            listView_SelectedContacts.ItemsSource = selectedContacts;
        }

        public WindowContacts(ObservableCollection<Contact> AllContacts, ObservableCollection<Contact> selectedContacts, AccountData accountData):this()
        {
            // TODO: Complete member initialization
            this.AllContacts = AllContacts;
            this.selectedContacts = selectedContacts;
            this._accountData = accountData;

            listView_AllContacts.ItemsSource = AllContacts;
            listView_SelectedContacts.ItemsSource = selectedContacts;
        }
        #endregion

        #region Dodanie  do listy Wybranych kontaktów
        private void AddToSelectedList()
        {

            foreach (Contact item in listView_AllContacts.SelectedItems)
            {
                if (!selectedContacts.Contains<Contact>(item))
                {
                    selectedContacts.Add(item);
                }
            }

            foreach (Contact item in selectedContacts)
            {
                AllContacts.Remove(item);
            }
        }

        private void btn_AddToSelected_Click(object sender, RoutedEventArgs e)
        {
            AddToSelectedList();
        }

        private void listView_AllContacts_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            AddToSelectedList();
        }
        #endregion

        #region Usuwnie z listy kontaktów
        private void RemoveFromSelectedList()
        {
            for (int i = listView_SelectedContacts.SelectedItems.Count; i > 0; i--)
            {
                Contact l = (Contact)listView_SelectedContacts.SelectedItems[i - 1];
                selectedContacts.Remove(l);
                AllContacts.Add(l);
            }   //selectedContacts.Remove(item);
        }
        private void btn_RemoveSelected_Click(object sender, RoutedEventArgs e)
        {
            RemoveFromSelectedList();
        }
        private void listView_SelectedContacts_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            RemoveFromSelectedList();
        }
        #endregion


        #region Zamykanie okna kontaktow

        private void ContactWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            CopyContactsToAllContacts();
            DialogResult = _result;
        }


        /// <summary>
        /// Należy wykonać przed zamknięciem formatki ponieważ muszimy przywrocic 
        /// wyszystkie kontakty do listy wszystkich kontaktow.
        /// </summary>
        private void CopyContactsToAllContacts()
        {
            foreach (Contact item in selectedContacts)
            {
                AllContacts.Add(item);
            }
            //czyszczenie filtra 
            _cv = (CollectionView)CollectionViewSource.GetDefaultView(AllContacts);
            _cv.Filter = null;
            //listView_AllContacts.Items.Refresh();
        }


        /// <summary>
        /// Zamykanie okna z menu głównego
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItemCloce_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = _result;
        }
        #endregion

        #region Przycisk OK
        private void btn_Ok_Click(object sender, RoutedEventArgs e)
        {
            _result = true;
            this.Close();
        }
        #endregion

        #region Otwieranie pliku kontaktów
        private void MenuItemOpenContactFile_Click(object sender, RoutedEventArgs e)
        {
            var dialogWindow = new Microsoft.Win32.OpenFileDialog {Filter = "Plik kontaktów (*.csv) | *.csv;"};


            var resoult = dialogWindow.ShowDialog();
            if (resoult == false)
            {
                return;
            }

            _accountData.KontaktsFile = dialogWindow.FileName;

            var contacts = CSVParser.LoadFile(dialogWindow.FileName);

            for (int i = 1; i < contacts.Count; i++)
            {
                string[] tmp = contacts[i];
                try
                {
                    if (!String.IsNullOrEmpty(tmp[28]))
                    {
                        if (AllContacts == null)
                            AllContacts = new ObservableCollection<Contact>();
                        AllContacts.Add(new Contact(tmp[28], tmp[0], tmp[26]));
                    }
                }
                catch (IndexOutOfRangeException)
                {
                }
            }
        }
        #endregion

        #region Filtrowanie listy kontaktów

        private bool FiltrAllContacts(Object obj)
        {
            if (TextBoxAllContactsFilter.Text.Length == 0)
                return true;

            var c = obj as Contact;

            return c.Name.IndexOf(TextBoxAllContactsFilter.Text, 0, StringComparison.CurrentCultureIgnoreCase) > -1;
        }

        private void TextBoxAllContactsFilter_KeyUp(object sender, KeyEventArgs e)
        {
            _cv = (CollectionView)CollectionViewSource.GetDefaultView(AllContacts);
            _cv.Filter = new Predicate<Object>(FiltrAllContacts);
            listView_AllContacts.Items.Refresh();
        }
        #endregion
        private void ContactWindow_Loaded(object sender, RoutedEventArgs e)
        {

        }
        #region Dodawanie nowego kontaktu
        private void MenuItemAddNew_Click(object sender, RoutedEventArgs e)
        {

        }
        #endregion
    }
}
